<template>
  <el-form  ref="dictionariesForm" size="large" :rules="rules" :model="form">
        <el-form-item label="父级" prop="parent">
          <el-input  v-model="form.parent"></el-input>
        </el-form-item>
        <el-form-item label="字典名" prop="dictionariesName">
          <el-input  v-model="form.dictionariesName"></el-input>
        </el-form-item>
        <el-form-item label="字典索引" prop="dictionariesKey">
          <el-input  v-model="form.dictionariesKey"></el-input>
        </el-form-item>
    <el-row  justify="center">
      <el-col :span="3"><el-button type="primary" @click="submitForm($refs.dictionariesForm)">保存</el-button></el-col>
      <el-col :span="3"><el-button @click="closeForm">关闭</el-button></el-col>
    </el-row>
  </el-form>
</template>
<script setup>
import {onMounted, onServerPrefetch, onUpdated, reactive, ref} from "vue";
import {addDictionaries, editDictionaries} from '../../../request/system/DictionariesApi.js'
import {ElMessage, ElNotification} from "element-plus";
import axios from "axios";
const insert=ref(true)
const form =reactive({
        dictionariesNo:'',
        parent:'',
        dictionariesName:'',
        dictionariesKey:''
})
const rest=(isInsert)=>{
  insert.value=isInsert
    dictionariesForm.value.resetFields()
}
const dictionariesForm=ref()
const fillForm=(row)=>{
  console.log(row)
  rest(false)
    form.dictionariesNo= row.dictionariesNo
    form.parent= row.parent
    form.dictionariesName= row.dictionariesName
    form.dictionariesKey= row.dictionariesKey
}

const emits = defineEmits(['closeDictionariesForm','submitDictionariesForm']);
const closeForm=()=>{
    emits("closeDictionariesForm")
}
const rules=reactive({
})

const submitForm = async (formEl) => {
  if (!formEl) return

  await formEl.validate((valid, fields) => {
    if (valid) {
      if(insert.value){
        addDictionaries(form).then((response)=>{
          if(response.state=="200"){
            ElMessage.success("新增成功")
            emits("submitDictionariesForm")
          }
        })
      }else {
        editDictionaries(form).then((response)=>{
          if(response.state=="200"){
            ElMessage.success("修改成功")
            emits("submitDictionariesForm")
          }
        })
      }
    } else {
      console.log('error submit!', fields)
    }
  })
}
defineExpose({ rest,fillForm })
onMounted(()=>{


})
</script>
<style scoped>

</style>
